﻿Imports Microsoft.WindowsMobile
Imports Microsoft.WindowsMobile.PocketOutlook
Imports Microsoft.WindowsMobile.PocketOutlook.MessageInterception
Imports System.Data.SqlServerCe

Public Class HomeForm

    Dim interceptor As MessageInterceptor

    Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click

    End Sub

    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
        Application.Exit()
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        Me.Hide()
    End Sub

    Private Sub EventsListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        EventView.Show()
        Me.Hide()
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        EventAdd.Show()
        Me.Hide()
    End Sub

    Private Sub HomeForm_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

        RemoveHandler interceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived

    End Sub

    Private Sub HomeForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'SMSRSVPDataSet.Events' table. You can move, or remove it, as needed.
        Me.EventsTableAdapter.Fill(Me.SMSRSVPDataSet.Events)


        'Set the messaging interceptor
        interceptor = New MessageInterceptor(InterceptionAction.NotifyAndDelete)
        interceptor.MessageCondition = New MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.StartsWith, "SMSRSVP ", False)
        AddHandler interceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived

    End Sub


    ' Public Delegate Sub MessageInterceptorEventHandler(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)


    Sub SmsInterceptor_MessageReceived(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)

        Dim msg As SmsMessage = e.Message

        If (Not msg.GetType.ToString = "Null") Then

            'Process the SMS text message 
            'OutputTextbox.Text = "" + msg.From.Address + "\r\n" + msg.Body + "\r\n\r\n" + OutputTextbox.Text;

            'ReceivedLabel.Text = msg.Received.ToString();
            'FromLabel.Text = msg.From.Address;
            'BodyLabel.Text = msg.Body;

            Dim bodyArr As String() = msg.Body.Split(" ")

            'OutlookSession session = new OutlookSession();

            'Write Data to DB
            SaveSMS(msg.Received, msg.From.Address, msg.From.Name, msg.Body, bodyArr(1))

        End If
    End Sub

    Private Sub SaveSMS(ByVal rec As DateTime, ByVal from As String, ByVal fromName As String, ByVal body As String, ByVal EventCode As String)

        Dim cnn As SqlCeConnection = New SqlCeConnection("Data Source=\Program Files\SMSRSVP\SMSRSVP.sdf;")
        Dim cmd As SqlCeCommand = New SqlCeCommand
        cmd.Connection = cnn
        cnn.Open()

        cmd.CommandText = "INSERT INTO RSVPs([EventCode],[From]) VALUES('" + EventCode + "','" + fromName + "')"

        Try 'In case we already have this item we can't insert, so we will ingnore for simplicity

            cmd.ExecuteNonQuery()

        Catch ex As Exception

        End Try

        
        cmd.Dispose()
        cnn.Dispose()

    End Sub

    '    private Sub EnabledCheckbox_CheckStateChanged(object sender, EventArgs e)

    '    If (EnabledCheckbox.Checked) Then

    '            interceptor.MessageReceived += SmsInterceptor_MessageReceived;

    '    Else

    '            interceptor.MessageReceived -= SmsInterceptor_MessageReceived;
    '    End If

    'End Sub

End Class
